﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class alter_route : BaseForm
    {
        public alter_route()
            : base()
        {
        }

        public alter_route(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbRouteName.DataSource = Utility.routes(dbContext);
            cbRouteName.DisplayMember = "Name";
        }

        private void cbRouteName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            if (cbRouteName.SelectedIndex > 0)
                sb.AppendLine("ALTER ROUTE [" + cbRouteName.Text + "]");
            else
                sb.AppendLine("ALTER ROUTE []");

            sb.AppendLine("WITH");
            if (tbServiceName.Text != string.Empty)
            {
                sb.AppendLine("   SERVICE_NAME = '" + tbServiceName.Text + "', ");
            }
            if (tbBroker.Text != string.Empty)
            {
                sb.AppendLine("   BROKER_INSTANCE = '" + tbBroker.Text + "', ");
            }
            if (tbLifetime.Text != string.Empty)
            {
                sb.AppendLine("   LIFETIME = " + tbLifetime.Text + ",");
            }
            sb.AppendLine("   ADDRESS = '" + tbAddress.Text + "' ");
            if (tbMirror.Text != string.Empty)
            {
                sb.AppendLine("  ,MIRROR_ADDRESS = '" + tbMirror.Text + "' ");
            }

            tbCommand.Text = sb.ToString();

        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbServiceName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbBroker_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbLifetime_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbAddress_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbMirror_TextChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}
